<?php

namespace app\common\controller;

use think\Controller;
use think\facade\Log;

/**
 * API controller 需要验证登录接口可继承
 * @package app\common\controller
 */
class ApiBase extends Controller
{
    protected $uid;
    protected $userInfo;

    protected function initialize()
    {
        $token = $this->request->header('Vauen-Token', 0);
        try {
            $this->userInfo = redis()->get($token);
            if (empty($this->userInfo)){
                return_ajax(0, '登录失效!请重新登录');
            }
            redis()->close();
        } catch (\RedisException $e) {
            Log::info('Exception Vauen-TokenError:'.$e->getMessage());
            return_ajax(0, '服务异常!稍后再试');
        }
        $this->userInfo = json_decode($this->userInfo, false);
        //登录用户信息转义标准的JSON格式返回前端友好支持
        $this->uid      = $this->userInfo->id;
        return true;
    }

}
